﻿using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Mapping.ByCode;
using Xenta.Enums;
using Xenta.Utils;

namespace Xenta.Entities
{
    /// <summary>
    /// Represents the message template entity.
    /// </summary>
    /// <remarks><pre>
    /// The entity regarding the disclosure level.
    /// 
    /// + Entire
    ///  + Extended
    ///   + Base
    ///    | - SenderID
    ///    | - From
    ///    | - To
    ///    | - BccTo
    ///    | - ReplyTo
    ///    | - Flags
    ///    | - UpdatedOn
    ///    | - CreatedOn
    ///    | - Translations(of the same level)
    ///    + Keys
    ///     | - Code
    ///     + Primary
    ///      | - EntityID
    /// 
    /// Note: upper level includes lower level.
    /// </pre></remarks>
    public class MessageTemplateEntity : AuditableEntity
    {
        #region Fields

        private ICollection<TMessageTemplateEntity> _translations;

        #endregion

        #region Ctors

        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public MessageTemplateEntity()
        {
            _translations = new List<TMessageTemplateEntity>();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the sender unique identifier.
        /// </summary>
        public virtual Guid SenderID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the code.
        /// </summary>
        public virtual string Code
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the sender address.
        /// </summary>
        public virtual string From
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the recipient address.
        /// </summary>
        public virtual string To
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the BCC recipient address.
        /// </summary>
        public virtual string BccTo
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the reply recipient address.
        /// </summary>
        public virtual string ReplyTo
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public virtual MessageTemplateFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Collections

        /// <summary>
        /// Gets or sets the template translations.
        /// </summary>
        public virtual ICollection<TMessageTemplateEntity> Translations
        {
            get
            {
                return _translations;
            }
            set
            {
                _translations = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the template translation.
        /// </summary>
        /// <param name="languageID">The language identifier.</param>
        /// <returns>The template translation.</returns>
        public virtual TMessageTemplateEntity Translation(int languageID)
        {
            return _translations.SingleOrDefault(x => x.Language.EntityID == languageID);
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level,
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Keys)
                obj["Code"] = Code;
            if(level >= DisclosureLevel.Base)
            {
                obj["SenderID"] = SenderID;
                obj["From"] = From;
                obj["To"] = To;
                obj["BccTo"] = BccTo;
                obj["ReplyTo"] = ReplyTo;
                obj["Flags"] = Flags.ToString();
                obj["Translations"] = (options.ContainsKey("TranslatedTo")
                    ? Translations.Where(x => options["TranslatedTo"].Contains(x.Language.Code))
                    : Translations)
                    .Map(x => x.Disclose(level, options));
            }
        }

        #endregion
    }

    /// <summary>
    /// The entity NHibernate mapping.
    /// </summary>
    public class MessageTemplateMapping : EntityMapping<MessageTemplateEntity>
    {
        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public MessageTemplateMapping()
        {
            Table("fwk_MessageTemplates");
            Id(x => x.EntityID, m =>
            {
                m.Column("TemplateID");
                m.Generator(Generators.Identity);
            });
            Property(x => x.SenderID, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.Code, m =>
            {
                m.Unique(true);
                m.Index("IX_fwk_MessageTemplates");
                m.NotNullable(true);
                m.Length(10);
                m.Column(c =>
                {
                    c.SqlType("nvarchar(10)");
                });
            });
            Property(x => x.From, m =>
            {
                m.NotNullable(true);
                m.Length(100);
                m.Column(c =>
                {
                    c.Name("[From]");
                    c.SqlType("nvarchar(100)");
                });
            });
            Property(x => x.To, m =>
            {
                m.NotNullable(true);
                m.Length(100);
                m.Column(c =>
                {
                    c.Name("[To]");
                    c.SqlType("nvarchar(100)");
                });
            });
            Property(x => x.BccTo, m =>
            {
                m.NotNullable(true);
                m.Length(100);
                m.Column(c =>
                {
                    c.SqlType("nvarchar(100)");
                });
            });
            Property(x => x.ReplyTo, m =>
            {
                m.NotNullable(true);
                m.Length(100);
                m.Column(c =>
                {
                    c.SqlType("nvarchar(100)");
                });
            });
            Property(x => x.Flags, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.CreatedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.UpdatedOn, m =>
            {
                m.NotNullable(true);
            });
            Bag(x => x.Translations, m =>
            {
                m.Table("fwk_TMessageTemplates");
                m.Key(k =>
                {
                    k.Column("TemplateID");
                    k.ForeignKey("FK_fwk_TMessageTemplates_fwk_MessageTemplates");
                    k.NotNullable(true);
                });
                m.Inverse(true);
                m.BatchSize(10);
            },
            a =>
            {
                a.OneToMany();
            });
            Cache(m =>
            {
                m.Usage(CacheUsage.ReadWrite);
            });
        }
    }
}
